﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.DAL.RecordCenter;
using Microsoft.SharePoint;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.DAL.DocumentCenter;
using RecordsDC = MS.InformationManagement.DAL.RecordCenter;

namespace MS.InformationManagement.BLL.GestionDeUsuarios
{
    public class AddUserGroupCommand : IBaseCommand
    {
        #region Fields

        string _targetUser;
        IEnumerable<Seccion> _secciones;

        #endregion

        #region Constructors

        public AddUserGroupCommand(string targetUser, IEnumerable<Seccion> seccion)
        {
            _targetUser = targetUser;
            _secciones = seccion;
        }

        #endregion

        #region Implemented

        public void Execute()
        {
            SPUser targetUser = SPContext.Current.Web.SiteUsers[_targetUser];

            foreach (var section in _secciones)
            {
                if (section.Grupo != null)
                {
                    SPGroup roleGroup = SPContext.Current.Web.Groups[section.Grupo];
                    roleGroup.AddUser(targetUser);
                    roleGroup.Update();
                }
            }
        }

        public void Undo()
        {
            SPUser targetUser = SPContext.Current.Web.SiteUsers[_targetUser];

            foreach (var section in _secciones)
            {
                if (section.Grupo != null)
                {
                    SPGroup roleGroup = SPContext.Current.Web.Groups[section.Grupo];
                    roleGroup.RemoveUser(targetUser);
                    roleGroup.Update();
                }
            }
        }

        #endregion

    }
}
